package com.neusoft.elm.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;
public class FoodDaoImpl implements FoodDao{
 
 private Connection con = null;
 private PreparedStatement pst = null;
 private ResultSet rs = null;
 @Override
 public List<Food> listFoodByBusinessId(Integer businessId) throws Exception {
 List<Food> list = new ArrayList<>();
 String sql = "select * from food where businessId=? order by foodId";
 try {
 con = DBUtil.getConnection();
 pst = con.prepareStatement(sql);
 pst.setInt(1, businessId);
 rs = pst.executeQuery();
 while(rs.next()) {
 Food food = new Food();
 food.setFoodId(rs.getInt("foodId"));
 food.setFoodName(rs.getString("foodName"));
 food.setFoodExplain(rs.getString("foodExplain"));
 food.setFoodImg(rs.getString("foodImg"));
 food.setFoodPrice(rs.getBigDecimal("foodPrice"));
 food.setBusinessId(rs.getInt("businessId"));
 food.setRemarks(rs.getString("remarks"));
 list.add(food);
 }
 }finally {
	 DBUtil.close(rs,pst);
	 }
	 return list;
	 }
	 
	@Override
	public Food getFoodById(Integer foodId) throws Exception {
		Food food = null;
		String sql = "select * from food where foodId=?";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, foodId);
			rs = pst.executeQuery();
			if(rs.next()) {
				food = new Food();
				food.setFoodId(rs.getInt("foodId"));
				food.setFoodName(rs.getString("foodName"));
				food.setFoodExplain(rs.getString("foodExplain"));
				food.setFoodImg(rs.getString("foodImg"));
				food.setFoodPrice(rs.getBigDecimal("foodPrice"));
				food.setBusinessId(rs.getInt("businessId"));
				food.setRemarks(rs.getString("remarks"));
			}
		}finally {
			DBUtil.close(rs,pst);
		}
		return food;
	}
	
	@Override
	public int saveFood(Food food) throws Exception {
		int result = 0;
		String sql = "insert into food(foodName,foodExplain,foodImg,foodPrice,businessId,remarks) values(?,?,?,?,?,?)";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, food.getFoodName());
			pst.setString(2, food.getFoodExplain());
			pst.setString(3, food.getFoodImg());
			pst.setBigDecimal(4, food.getFoodPrice());
			pst.setInt(5, food.getBusinessId());
			pst.setString(6, food.getRemarks());
			result = pst.executeUpdate();
		}finally {
			DBUtil.close(pst);
		}
		return result;
	}
	
	@Override
	public int updateFood(Food food) throws Exception {
		int result = 0;
		String sql = "update food set foodName=?,foodExplain=?,foodImg=?,foodPrice=?,remarks=? where foodId=?";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, food.getFoodName());
			pst.setString(2, food.getFoodExplain());
			pst.setString(3, food.getFoodImg());
			pst.setBigDecimal(4, food.getFoodPrice());
			pst.setString(5, food.getRemarks());
			pst.setInt(6, food.getFoodId());
			result = pst.executeUpdate();
		}finally {
			DBUtil.close(pst);
		}
		return result;
	}
	
	@Override
	public int removeFood(Integer foodId) throws Exception {
		int result = 0;
		String sql = "delete from food where foodId=?";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, foodId);
			result = pst.executeUpdate();
		}finally {
			DBUtil.close(pst);
		}
		return result;
	}
}
